// pages/detail/detail.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        desk: null,
        list: [],
        out: [],
        side: [],
        menu: false,
        path: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        const file = require('../../lib/file.js');
        const dl = new file.file();
        dl.loadFont();
        dl.fill(function () {
            that.setData({
                path: dl.all
            })
        });
        let index = options.index;
        let desk = app.globalData.desks[index];
        this.setData({
            index: index,
            desk: desk
        })
        this.fresh();
    },
    getCards(list, callback) {
        const that=this;
        wx.cloud.callFunction({
            name: "getCards",
            data: {
                list: list
            },
            success: res => {
                callback(res.result.data);
                wx.hideLoading();
                that.calNum();
            },
        })
    },
    ansys(list, desk) {
        list = list.concat();
        const Card = require("../../lib/card.js");
        let cards = [];
        let r = this.getScale();
        for (let i in desk) {
            let tCard = desk[i];
            let temp = [];
            for (let j in list) {
                if (tCard.password == list[j]._id) {
                    var tempCard = list[j];
                    tempCard.num = tCard.num;
                    list.splice(j, 1);
                    break;
                }
            }
            for (let n = 0; n < tempCard.num; n++) {
                let card = new Card.Card(tempCard, r, true,true);
                temp.push(card);
            }
            cards.push(temp);
        }
        return cards;
    },
    getScale() {
        let w = 0.864 * wx.getSystemInfoSync().windowWidth / 5;
        let r = w / 600;
        return r;
    },
    goTo(e) {
        if (this.data.menu) {
            this.hideMenu();
            return;
        }
        let password = e.currentTarget.dataset.password;
        wx.navigateTo({
            url: '../../pages/cardIfm/cardIfm?password=' + password,
        })
    },
    showMenu(e) {
        if (this.data.menu) {
            this.hideMenu();
            const that = this;
            setTimeout(function () {
                that.showMenu(e);
            }, 100);
            return;
        }
        let password = e.currentTarget.dataset.password;
        let type = e.currentTarget.dataset.type;
        let name = e.currentTarget.dataset.name;
        this.setData({
            menu: true,
            event: [password, type]
        })
        wx.setNavigationBarTitle({
            title: name,
        })
    },
    hideMenu() {
        const that=this;
        this.setData({
            menu: false,
            event: [null, null]
        })
        wx.setNavigationBarTitle({
            title: that.data.desk.name,
        })
    },
    delCard() {
        let password = this.data.event[0];
        let type = this.data.event[1];
        let list = this.data[type]
        for (let i in list) {
            let card = list[i][0];
            if (password == card.ifm._id) {
                list[i].splice(-1, 1);
                if (list[i].length == 0) {
                    list.splice(i, 1);
                    this.setData({
                        menu: false
                    })
                }
                break;
            }
        }
        let temp = new Object();
        temp[type] = list;
        this.setData(temp);
        this.unzip();
    },
    addCard() {
        let password = this.data.event[0];
        let type = this.data.event[1];

        if (type == "side" && this.data.desk.cardNum[3] >= 15) {
            wx.showModal({
                title: '超出数量限制',
                content: '副卡组最多只能有15张',
            })
            return;
        }
        if (type == "list" && this.data.desk.cardNum[1] >= 70) {
            wx.showModal({
                title: '超出数量限制',
                content: '卡组最多只能有70张',
            })
            return;
        }
        if (type == "out" && this.data.desk.cardNum[2] >= 15) {
            wx.showModal({
                title: '超出数量限制',
                content: '额外卡组最多只能有70张',
            })
            return;
        }

        let type2=type=="side"?["list","out"]:["side"];
        let list = this.data[type]
        let num=0;
        type2.forEach((t,i,type)=> {
            let tList=this.data[t];
            tList.forEach((card_,i,list_)=>{
                if (card_[0].ifm._id==password) {
                    num=card_.length;
                }
            })
        })
        for (let i in list) {
            let r = this.getScale();
            let card = list[i][0].born(r);
            if (password == card.ifm._id) {
                if ((list[i].length+num) > 2) {
                    wx.showModal({
                        title: '超出数量限制',
                        content: (num>0 && type!='side')?'请检查额外卡组':'同名卡最多只能3张',
                    })
                    return;
                }
                list[i].splice(list[i].length, 0, card);
                break;
            }
        }
        let temp = new Object();
        temp[type] = list;
        this.setData(temp);
        this.unzip();
    },
    unzip() {
        let main = this.data.list;
        let out = this.data.out;
        let side = this.data.side;

        let piker = function (list) {
            let temp = [];
            for (let i in list) {
                let tc = list[i][0];
                let card = {
                    password: tc.ifm._id,
                    name: tc.ifm.name,
                    level: tc.ifm.level,
                    attack: tc.ifm.attack,
                    type: tc.ifm.type,
                    type2: tc.ifm.type2,
                    type3: tc.ifm.type3,
                    num: list[i].length
                };
                temp.push(card);
            }
            return temp;
        }

        let main_res = piker(main);
        let out_res = piker(out);
        let side_res = piker(side);

        app.globalData.desks[this.data.index].list = main_res;
        app.globalData.desks[this.data.index].out = out_res;
        app.globalData.desks[this.data.index].side = side_res;
        app.globalData.desks[this.data.index].update();

        this.calNum();
    },
    fresh() {
        const that = this;
        let index = this.data.index;
        let desk = app.globalData.desks[index];
        wx.setNavigationBarTitle({
            title: desk.name,
        });

        let list = [];
        let cardList = desk.list.concat(desk.out).concat(desk.side);
        for (let i in cardList) {
            list.push(cardList[i].password);
        }
        this.getCards(list, function (res) {
            let main = that.ansys(res, desk.list);
            let out = that.ansys(res, desk.out);
            let side = that.ansys(res, desk.side);
            that.setData({
                list: main,
                out: out,
                side: side
            })
        });
    },
    onShow() {
        this.fresh();
    },
    calNum() {
        const that=this;
        const cal = function (list) {
            let res=0;
            list.forEach((l, i) => {
                res+=l.length;
            })
            return res;
        }

        let main = cal(this.data.list);
        let out = cal(this.data.out);
        let side = cal(this.data.side);

        this.setData({
            num: [main,out,side]
        })
    }
})